WireIt.JavascriptContainer = function(options, layer){
	WireIt.JavascriptContainer.superclass.constructor.call(this, options, layer);
   
   this.buildTextArea(options.scriptText || "function sampleScript (a,b,c) {\n\n  return a+b+c;\n}");
   
   this.createTerminals();
   
   // Reposition the terminals when the Workflow box is being resized
   this.ddResize.eventResize.subscribe(function(e, args) {
      this.positionTerminals();
      YAHOO.util.Dom.setStyle(this.textarea, "height", (args[0][1]-70)+"px");
   }, this, true);
	
}

YAHOO.lang.extend(WireIt.JavascriptContainer, WireIt.WebUIContainer, {
  
   
   /**
    * Create the textarea for the javascript code
    * @method buildTextArea
    * @Input {String} codeText
    */
   buildTextArea: function(codeText) {

      this.textarea = WireIt.cn('textarea', null, {width: "90%", height: "70px", border: "0", padding: "5px"}, codeText);
      this.setBody(this.textarea);

      YAHOO.util.Event.addListener(this.textarea, 'change',this.createTerminals, this, true);
	  
      
   },
   
   /**
    * Create (and re-create) the terminals with this.nInputs input terminals
    * @method createTerminals
    */
   createTerminals: function() {
		if(this.textarea.value.indexOf("function")>=0){  
		if (this.inputs) {
			this.oldInputs = this.inputs;
		}
		else {
			this.oldInputs = new Array;
		}
	
	//read input terminals from textarea
	      var match = (this.textarea.value.replace(new RegExp("[\\n\\s]*","g"),'')).match((/^function[\w\.]*\((.*)\){/));
		  this.name = (this.textarea.value.replace(new RegExp("[\\n\\s]*","g"),'')).match((/^function([\w\.]*)\(/))[1];
		  this.setName(this.name);	  
		  var sInputList = match ? match[1] : "";
	      this.inputs = sInputList.split(','); // array of input terminals
	      var nInputs = (sInputList=="") ? 0 : this.inputs.length;
		  this.nOutputs = 1;
		  
		  if (this.inputs.toString() != this.oldInputs.toString()) {
			this.removeAllTerminals();	  
			// create input terminals
			      for(var i = 0 ; i < nInputs ; i++) {
					 var term = this.addTerminal({
						xtype: "WireIt.util.WebUITerminalInput",
						"name": this.inputs[i].replace(new RegExp("[\\n\\s]*","g"),''),
						"ddConfig": {
							"type": "input:script",
							"allowedTypes":["output:any"]
						},
					});
			      }		
			// create output terminal
				   this.outputTerminal = this.addTerminal({
									xtype: "WireIt.util.WebUITerminalOutput",
									"name": "out",
									"ddConfig": {
										"type": "output:script",
										"allowedTypes":["input:any"]
									},
								});      
					//alert(this.outputTerminal.options.ddConfig.allowedTypes);
					this.nInputs=nInputs;
				  
			      if (this.terminals.length > 0) {
			        this.positionTerminals();
			      }
			
			      // Declare the new terminals to the drag'n drop handler (so the wires are moved around with the container)
			      this.dd.setTerminals(this.terminals);
			}
		}
		else {
			if (this.inputs) {
				this.oldInputs = this.inputs;
			}
			else {
				this.oldInputs = new Array;
			}
			if (this.outputs) {
				this.oldOutputs = this.outputs;
			}
			else {
				this.oldOutputs = new Array;
			}
			var match = (this.textarea.value.replace(new RegExp("[\\n\\s]*","g"),'')).match((/^([\w\.]*)\((.*)\):\((.*)\){/));
			this.name = match[1];
			this.setName(this.name);
			var sInputList = match ? match[2] : "";
			var sOutputList = match ? match[3] : "";
			
			this.inputs = sInputList.split(','); // array of input terminals
	      	this.outputs= sOutputList.split(',');
			var nInputs = (sInputList=="") ? 0 : this.inputs.length;
			var nOutputs = (sOutputList=="") ? 0 : this.outputs.length;
			
			if ((this.inputs.toString() != this.oldInputs.toString())||(this.outputs.toString() != this.oldOutputs.toString())) {
				this.removeAllTerminals();	
				// create input terminals
			      for(var i = 0 ; i < nInputs ; i++) {
					 var term = this.addTerminal({
						xtype: "WireIt.util.WebUITerminalInput",
						"name": this.inputs[i].replace(new RegExp("[\\n\\s]*","g"),''),
						"ddConfig": {
							"type": "input:script",
							"allowedTypes":["output:any"]
						},
					});
			      }
			}
			     // create output terminals 
				for (var i = 0; i < nOutputs; i++) {
					var term = this.addTerminal({
						xtype: "WireIt.util.WebUITerminalOutput",
						"name": this.outputs[i].replace(new RegExp("[\\n\\s]*", "g"), ''),
						"ddConfig": {
							"type": "output:script",
							"allowedTypes": ["output:any"]
						},
					});
				}
				this.nInputs=nInputs;
				this.nOutputs = nOutputs;
				  
			    if (this.terminals.length > 0) {
			        this.positionTerminals();
			    }
			
			    // Declare the new terminals to the drag'n drop handler (so the wires are moved around with the container)
			    this.dd.setTerminals(this.terminals);
				
			
		}
    },
   
   /**
    * Reposition the terminals
    * @method positionTerminals
    */
   positionTerminals: function() {
      var width = WireIt.getIntStyle(this.el, "width");
      //input intervals	  
	  if (this.nInputs > 0) {
		  var inputsInterval = Math.floor(width/(this.nInputs+1));
		  for(var i = 1 ; i < (this.terminals.length-this.nOutputs+1) ; i++) {
			 var term = this.terminals[i-1];
			 WireIt.sn(term.el, null, {position: "absolute", top: "-15px", left: (inputsInterval*(i)-15)+"px"});
		  }
	  }

	//output intervals
		  if (this.nOutputs > 0) {
		  var outputsInterval = Math.floor (width/(this.nOutputs+1));
			if (this.nInputs == 0){
					for(var i = 1 ; i < (this.nOutputs+1) ; i++) {
					var term = this.terminals[i-1];
					WireIt.sn(term.el, null, {position: "absolute", bottom: "-15px", left: (outputsInterval*(i)-15)+"px"});
			  } 
			}
			else {
				for(var i = 1 ; i < (this.nOutputs+1) ; i++) {
					var term = this.terminals[this.nInputs + i-1];
					WireIt.sn(term.el, null, {position: "absolute", bottom: "-15px", left: (outputsInterval*(i)-15)+"px"})
			  } 
			} 
		} 

    },
   
   /**
    * Extend the getConfig to add the "codeText" property
    * @method getConfig
    */
   getConfig: function() {
      var obj = WireIt.JavascriptContainer.superclass.getConfig.call(this);
	  obj.scriptText = this.textarea.value;
	  obj.name = this.name;
      return obj;
   },
   
   initType: function(){
   //a stub
   },
	
	setName: function (name){
		this.ddHandle.removeChild(this.title);
		this.title = WireIt.cn('span', null, null, name);
		this.ddHandle.appendChild(this.title);
		this.title == "JavaScript";
	},
});